from src.frame_extract import FrameExtractor
from src.video_analysis_module import VideoAnalysis
import logging
import os   

from src.config_reader import ConfigReader
# 配置日志  
logging.basicConfig(level=logging.INFO)

def extract_frame(video_name):
    extractor = FrameExtractor()
    extractor.process(video_name)

def analysis_video(video_name, time_point, question):
    try:
        # 1. 创建分析器实例
        analyzer = VideoAnalysis(video_name=video_name)

        # 2. 运行分析 (如果需要，可以设置 force_rerun_all=True)
        success = analyzer.run_full_analysis(force_rerun_all=False)

        if success:
            print("分析完成。")

            # 3. 进行对话
            answer = analyzer.ask(time_point, question)
            print(f"回答 ({time_point}s): {answer}")

        else:
            print("分析过程中出现错误，请检查日志。")

    except ValueError as e:
        print(f"初始化错误: {e}")
    except FileNotFoundError as e:
        print(f"文件或目录未找到: {e}")
    except Exception as e:
        print(f"发生未知错误: {e}")
        logging.exception("An error occurred in the main script.")
        
        
if __name__ == '__main__':
    # video_name = "456998983-1-16.mp4"
    # video_name = "456998365-1-192.mp4"
    video_name = "456998289-1-192.mp4"
    # video_name = "456993416-1-192.mp4"
    # extract_frame(video_name) # 提取视频帧
    analysis_video(video_name, 40, "烤箱里是什么？") # 分析视频
    